import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    goods_id: '',
    goods_deltail: {},
    active: 0,
    addressInfo: null
  }

  methods = {
    onChange(event) {
      console.log(event.detail.name)
    },

    preview(current) {
      wepy.previewImage({
        urls: this.goods_deltail.pics.map(item => item.pics_big),
        current: current
      })
    },

    async chooseAddress() {
      const res = await wepy.chooseAddress().catch(err => err)

      if (res.errMsg !== 'chooseAddress:ok') {
        return wepy.baseToast('获取收货地址失败！')
      }

      this.addressInfo = res
      wepy.setStorageSync('address', res)
      this.$apply()
    },

    goToCart() {
      this.$parent.addCartGoods(this.goods_deltail)
      wepy.showToast({
        title: '加入购物车成功！',
        icon: 'success'
      })
    }
  }

  computed = {
    addressStr() {
      if (this.addressInfo === null) {
        return '请选择收货地址'
      }
      const addr = this.addressInfo
      const str =
        addr.provinceName + addr.cityName + addr.countyName + addr.detailInfo
      return str
    },
    total(){
      return this.$parent.globalData.count
    }
  }

  onLoad(options) {
    // console.log(options)
    this.goods_id = options.goods_id
    this.getGoodsDetail()
  }

  async getGoodsDetail() {
    const { data: res } = await wepy.get('/goods/detail', {
      goods_id: this.goods_id
    })
    // console.log(res)
    if (res.meta.status !== 200) {
      return wepy.baseToast()
    }
    this.goods_deltail = res.message
    // console.log(this.goods_deltail.pics);
    this.$apply()
  }
}
